using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using jqMvcGrid.Models;

namespace jqMvcGrid.Controllers
{
    public class CategoriesController : Controller
    {
        private ICategoryRepository _repository = new CategoryRepository();

        //
        // GET: /Categories/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(int page, int rows, string search, string sidx, string sord)
        {
            var repository = new CategoryRepository();
            var model = from entity in repository.Index().OrderBy(sidx + " " + sord)
                        select new
                        {
                            Edit = "<a style='color:#5c87b2' href='" + "Categories/Edit/" + entity.CategoryID + "'>Edit</a>",
                            Details = "<a style='color:#5c87b2' href='" + "Categories/Details/" + entity.CategoryID + "'>Details</a>",
                            CategoryID = entity.CategoryID,
                            //CategoryName = "<a href=''>" + entity.CategoryName + "</a>",
                            CategoryName = entity.CategoryName,
                            Description = entity.Description
                        };
            return Json(model.ToJqGridData(page, rows, null, search,
                new[] { "CategoryName", "Description" }));
        }

        //
        // GET: /Categories/Details/5

        public ActionResult Details(int id)
        {
            return View(_repository.Details(id));
        }

        //
        // GET: /Categories/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Categories/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Category category, FormCollection collection)
        {
            try
            {
                // Validate
                if (!Validate(category))
                    return View();

                // Create
                if (!_repository.Create(category))
                    return View();

                // Success
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Categories/Edit/5

        public ActionResult Edit(int id)
        {
            return View(_repository.Details(id));
        }

        //
        // POST: /Categories/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Category category, FormCollection collection)
        {
            try
            {
                // Validate
                if (!Validate(category))
                    return View(category);

                // Edit
                if (!_repository.Edit(category))
                    return View(category);

                //  Success
                return RedirectToAction("Index");
            }
            catch
            {
                return View(category);
            }
        }

        protected bool Validate(Category category)
        {
            if (string.IsNullOrEmpty(category.CategoryName))
                ModelState.AddModelError("CategoryName", "CategoryName is required!");
            return ModelState.IsValid;
        }
    }
}
